//
//  DetailViewController.m
//  WorkManager
//
//  Created by iD Student on 7/17/12.
//  Copyright (c) 2012 iD Student. All rights reserved.
//

#import "DetailViewController.h"

@implementation DetailViewController
@synthesize appointmentReport;
@synthesize dateLabel;
@synthesize timeAndDateCell;
@synthesize workRequestDetailsTextView;
@synthesize workReportTextView;
@synthesize rateField;
@synthesize amountField;
@synthesize hoursWorkedField;
@synthesize paymentTypeCell;
@synthesize signatureCell;
@synthesize timeLabel;
@synthesize nameField;
@synthesize emailField;
@synthesize phoneField;
@synthesize addressField;
@synthesize townField;
@synthesize stateField;
- (void)dealloc
{
    [nameField release];
    [phoneField release];
    [addressField release];
    [townField release];
    [stateField release];
    [dateLabel release];
    [timeLabel release];
    [timeAndDateCell release];
    [emailField release];
    [workRequestDetailsTextView release];
    [workReportTextView release];
    [rateField release];
    [amountField release];
    [hoursWorkedField release];
    [paymentTypeCell release];
    [signatureCell release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
}

- (void)updateTimeAndDateText
{
    //Create the calendar in which to place the date
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    
    //Create the time components to store the components of now
    NSDateComponents *dateComponents = [gregorian components:(NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit| NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[appointmentReport date]];
    
    //Create a string for the date
    NSString *dateString = [NSString stringWithFormat:@"%d/%d/%d", [dateComponents month] , [dateComponents day], [dateComponents year]];
    [dateLabel setText:dateString];
    
    //Create a string for the time
    //Bring the hour under twelve if necessary
    NSString *amPmString;
    if([dateComponents hour]>12)
    {
        [dateComponents setHour:([dateComponents hour]-12)];
        amPmString = @"PM";
    }
    else {
        amPmString = @"AM";
    }
    
    NSString *timeString;
    if([dateComponents minute]<10)
        timeString = [NSString stringWithFormat:@"%d:0%d %@", [dateComponents hour] , [dateComponents minute] , amPmString];
    else
        timeString = [NSString stringWithFormat:@"%d:%d %@", [dateComponents hour] , [dateComponents minute] , amPmString];
    
    [timeLabel setText:timeString];
}

- (void)viewWillAppear:(BOOL)animated
{
    if ([appointmentReport name]!=nil)
        [nameField setText:[appointmentReport name]];
    
    if ([appointmentReport phone]!=nil)
        [phoneField setText:[appointmentReport phone]];
    
    if ([appointmentReport email]!=nil)
        [emailField setText:[appointmentReport email]];
    
    if ([appointmentReport address]!=nil)
        [addressField setText:[appointmentReport address]];
    
    if ([appointmentReport town]!=nil)
        [townField setText:[appointmentReport town]];
    
    if ([appointmentReport state]!=nil)
        [stateField setText:[appointmentReport state]];
    
    if ([appointmentReport workRequestDetails]!=nil)
        [workRequestDetailsTextView setText:[appointmentReport workRequestDetails]];
    
    if ([appointmentReport workReport]!=nil)
        [workReportTextView setText:[appointmentReport workReport]];
    
    if ([appointmentReport rate]!=nil)
        [rateField setText:[appointmentReport rate]];
    
    if ([appointmentReport amount]!=nil)
        [amountField setText:[appointmentReport amount]];
    
    if ([appointmentReport hoursWorked]!=nil)
        [hoursWorkedField setText:[appointmentReport hoursWorked]];
    
    switch ([appointmentReport paymentType]) {
        case 1:
            [paymentTypeCell.detailTextLabel setText:@"Cash"];
            break;
        case 2:
            [paymentTypeCell.detailTextLabel setText:@"Check"];
            break;
        case 3:
            [paymentTypeCell.detailTextLabel setText:@"Credit"];
            break;
        case 4:
            [paymentTypeCell.detailTextLabel setText:@"Paypal"];
            break;
        case 5:
            [paymentTypeCell.detailTextLabel setText:@"Other"];
            break;
            
        default:
            [paymentTypeCell.detailTextLabel setText:@""];
            break;
    }
    
    [self updateTimeAndDateText];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self saveOnExit];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)resignFirstResponders:(id)sender {
    [nameField resignFirstResponder];
    [phoneField resignFirstResponder];
    [emailField resignFirstResponder];
    [addressField resignFirstResponder];
    [townField resignFirstResponder];
    [stateField resignFirstResponder];
    [rateField resignFirstResponder];
    [amountField resignFirstResponder];
    [hoursWorkedField resignFirstResponder];
}

- (void)saveOnExit
{
    [appointmentReport setName:[nameField text]];
    [appointmentReport setPhone:[phoneField text]];
    [appointmentReport setEmail:[emailField text]];
    [appointmentReport setAddress:[addressField text]];
    [appointmentReport setTown:[townField text]];
    [appointmentReport setState:[stateField text]];
    [appointmentReport setWorkRequestDetails:[workRequestDetailsTextView text]];
    [appointmentReport setWorkReport:[workReportTextView text]];
    [appointmentReport setRate:[rateField text]];
    [appointmentReport setAmount:[amountField text]];
    [appointmentReport setHoursWorked:[hoursWorkedField text]];
}

//Get ready to move over
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDateSelector"]) {
        [[segue destinationViewController] setAppReport:self.appointmentReport];
    }
    else if ([[segue identifier] isEqualToString:@"paymentTypeEdit"]) {
        [[segue destinationViewController] setAppointmentReport:self.appointmentReport];
    }
    else if ([[segue identifier] isEqualToString:@"initialView"]) {
        [[segue destinationViewController] setAppointmentReport:self.appointmentReport];
    }
    [timeAndDateCell setSelected:NO];
    [timeAndDateCell setEditing:NO];
    [timeAndDateCell setHighlighted:NO];
    [paymentTypeCell setSelected:NO];
    [paymentTypeCell setEditing:NO];
    [paymentTypeCell setHighlighted:NO];
    [signatureCell setSelected:NO];
    [signatureCell setEditing:NO];
    [signatureCell setHighlighted:NO];
}


#pragma mark ----------Text Field Delegation Methods----------

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [textField setTextColor:[UIColor blackColor]];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
}
@end
